home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 January / PCWorld_2007-01_cd.bin / v cisle / hotkey / AutoHotkey104504_Install.exe / Extras / Run this to install syntax highlighting for UltraEdit.ahk < prev    next >
Text File  |  2006-08-29  |  10KB  |  251 lines

  1. ;##############################################################################
  2. ;#
  3. ;#  Add or update syntax highlighting for AutoHotKey scripts in UltraEdit
  4. ;#
  5. ;#  Mod of a script done by Tekl (although not much has survived)
  6. ;#  Mod done by toralf, 2005-11-14
  7. ;#
  8. ;#  Tested with: AHK 1.0.40.06, UltraEdit 11.10a
  9. ;#
  10. ;#  Requirements
  11. ;#    - Syntax files for AHK in one directory
  12. ;#    - UltraEdit uses standard file for highlighting => wordfile.txt
  13. ;#
  14. ;#  Customize:
  15. ;#    - The default color for strings is gray, change it to any color
  16. ;#         you want to have "string" to appeer => Extra->Option->syntaxhiglighting
  17. ;#    - Change the default color for up to 8 keyword groups
  18. ;#         => Extra->Option->syntaxhiglighting
  19. ;#    -specify up to 8 syntax files, each containing one keyword per line
  20. ;#         => you can add your own files, for keywords that you want to highlight
  21. ;#            Personally I use 3 additional: Operators, Separators and Special
  22. ;#            The content of these files is posted further down
  23. ;#
  24.  
  25. ; Specify a list of up to 8 syntax files; the order influences the color given to them by UE by default
  26. SyntaxFileNameList = CommandNames|Keywords|Variables|Functions|Keys|Operators|Separators|Special
  27. ;Default colors in UE:  blue     |red     |orange   |green    |brown|blue    |blue      |blue
  28.  
  29. SyntaxExtention = .txt
  30.  
  31. ;#############   END of Customization Area   ##################################
  32.  
  33. ;#############   Ask and Check for valid input  ###############################
  34.  
  35. RegRead, UeditPath, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\UEDIT32.exe,Path
  36. IfNotExist, %UeditPath%\uedit32.exe
  37.   {
  38.     UeditPath = %A_ProgramFiles%\UltraEdit
  39.     IfNotExist, %UeditPath%\uedit32.exe
  40.       {
  41.         FileSelectFolder, UeditPath,*%A_ProgramFiles%, 0, Select UltraEdit program-folder
  42.         IfNotExist, %UeditPath%\uedit32.exe
  43.           {
  44.             MsgBox UltraEdit cannot be found.
  45.             ExitApp
  46.           }
  47.       }
  48.   }
  49.  
  50. UEini = %APPDATA%\IDMComp\UltraEdit\uedit32.ini
  51. IfNotExist, %UEini%
  52.   {
  53.     UEini = %A_WinDir%\uedit32.ini
  54.     IfNotExist, %UEini%
  55.         FileSelectFile, UEini, 1, %A_ProgramFiles%\UltraEdit, Select UltraEdit INI-File, *.ini
  56.   }
  57. IniRead, UEwordfile, %UEini%, Settings, Language File
  58. If UEwordfile = Error
  59.   {
  60.     MsgBox INI-File "%UEini%" is missing the Key "Language File".
  61.     ExitApp
  62.   }
  63.  
  64. ;Search or ask for Wordfile, when it doesn't exist -> exit
  65. UEwordfile = %UeditPath%\wordfile.txt
  66. IfNotExist, %UEwordfile%
  67.   {
  68.     FileSelectFile, UEwordfile, 1, %A_ProgramFiles%, Select UltraEdit wordfile, *.txt
  69.     IfNotExist, %UEwordfile%
  70.       {
  71.         MsgBox, 16,, UltraEdit Wordfile cannot be found.
  72.         ExitApp
  73.       }
  74.   }
  75.  
  76. ; Discover where AutoHotkey and its related files reside:
  77. RegRead, ahkpath, HKLM, SOFTWARE\AutoHotkey, InstallDir
  78. if (ErrorLevel or not FileExist(ahkpath . "\AutoHotkey.exe"))  ; Not found, so try best-guess instead.
  79.     SplitPath, A_AhkPath,, ahkpath
  80.  
  81. PathSyntaxFiles = %ahkpath%\AutoHotkey\Extras\Editors\Syntax
  82. IfNotExist, %PathSyntaxFiles%
  83.   {
  84.     PathSyntaxFiles = %A_ProgramFiles%\AutoHotkey\Extras\Editors\Syntax
  85.     IfNotExist, %PathSyntaxFiles%
  86.       {
  87.         FileSelectFolder, PathSyntaxFiles, *%A_ProgramFiles%,0, Select Folder "AutoHotkey\Extras\Editors\Syntax"
  88.         IfNotExist, %PathSyntaxFiles%
  89.           {
  90.             MsgBox, 16,, Folder containing syntax files not found.
  91.             ExitApp
  92.           }
  93.       }
  94.   }
  95.  
  96. MissingFile =
  97. FileCount = 0
  98. Loop, Parse, SyntaxFileNameList, |
  99.   {
  100.     FileCount += 1
  101.     IfNotExist, %PathSyntaxFiles%\%A_LoopField%%SyntaxExtention%
  102.         MissingFile = %MissingFile%`n%A_LoopField%%SyntaxExtention%
  103.   }
  104. If MissingFile is not Space
  105.   {
  106.     MsgBox, 16,, AHK Syntax file(s)`n%MissingFile%`n`ncannot be found in`n`n%PathSyntaxFiles%\.
  107.     ExitApp
  108.   }
  109. If FileCount > 8
  110.   {
  111.     MsgBox, 16,, You have specified %FileCount% Syntax files.`nOnly 8 are supported be UltraEdit.`nPlease shorten the list.
  112.     ExitApp
  113.   }
  114.  
  115. ;Check the number of languages in the current wordfile, if more than 19 without AHK -> exit
  116. NumberOfLanguages = 0
  117. Loop, Read, %UEwordfile%
  118.   {
  119.     StringLeft, WFdef, A_LoopReadLine, 2
  120.     If WFdef = /L
  121.       {
  122.         StringSplit, WFname, A_LoopReadLine, "      ;"
  123.         LanguageName = %WFname2%
  124.         If LanguageName <> AutoHotkey
  125.             NumberOfLanguages += 1
  126.       }
  127.   }
  128. If NumberOfLanguages > 19
  129.   {
  130.     MsgBox, 48,, The wordfile has %NumberOfLanguages% syntax-schemes. UltraEdit does only support 20 schemes.`nPlease Delete schemes from the file!
  131.     ExitApp
  132.   }
  133.  
  134. ;#############   Read keywords from syntax files into arrays   ################
  135.  
  136. Loop, Parse, SyntaxFileNameList, |  ;Read all syntax files
  137.   {
  138.     SyntaxFileName = %A_LoopField%
  139.     Gosub, ReadSyntaxFromFile       ;SyntaxFileName will become string with keywords
  140.   }
  141.  
  142. ;#############   Build language specific highlight for AHK   ##################
  143.  
  144. StrgAHKwf = "AutoHotkey" Nocase
  145. StrgAHKwf = %StrgAHKwf% Line Comment = `;
  146. StrgAHKwf = %StrgAHKwf% Line Comment Preceding Chars = [~``]     ;to Escape Escaped ;
  147. StrgAHKwf = %StrgAHKwf% Escape Char = ``
  148. StrgAHKwf = %StrgAHKwf% String Chars = "                                                   ;"
  149. StrgAHKwf = %StrgAHKwf% Block Comment On = /*
  150. StrgAHKwf = %StrgAHKwf% Block Comment Off = */
  151. StrgAHKwf = %StrgAHKwf% File Extensions = ahk`n
  152. StrgAHKwf = %StrgAHKwf%/DeLimiters = *~`%+-!^&(){}=|\/:"'``;<>%A_Tab%,%A_Space%.`n         ;"
  153. StrgAHKwf = %StrgAHKwf%/Indent Strings = "{" ":" "("`n
  154. StrgAHKwf = %StrgAHKwf%/Unindent Strings = "}" "Return" "Else" ")"`n
  155. StrgAHKwf = %StrgAHKwf%/Open Fold Strings = "{"`n
  156. StrgAHKwf = %StrgAHKwf%/Close Fold Strings = "}"`n
  157. StrgAHKwf = %StrgAHKwf%/Function String = "`%[^t ]++^(:[^*^?BbCcKkOoPpRrZz0-9- ]++:*`::^)"`n   ; Hotstrings
  158. StrgAHKwf = %StrgAHKwf%/Function String 1 = "`%[^t ]++^([a-zA-Z0-9 #!^^&<>^*^~^$]+`::^)"`n     ; Hotkeys
  159. StrgAHKwf = %StrgAHKwf%/Function String 2 = "`%[^t ]++^([a-zA-Z0-9Σ÷ⁿ▀#_@^$^?^[^]]+:^)"`n      ; Subroutines
  160. StrgAHKwf = %StrgAHKwf%/Function String 3 = "`%[^t ]++^([a-zA-Z0-9Σ÷ⁿ▀#_@^$^?^[^]]+(*)^)"`n    ; Functions
  161. StrgAHKwf = %StrgAHKwf%/Function String 4 = "`%[^t ]++^(#[a-zA-Z]+ ^)"`n                       ; Directives
  162.  
  163. Loop, Parse, SyntaxFileNameList, |      ;Add the keywords from syntax strings into their Sections
  164.   {
  165.     StrgAHKwf = %StrgAHKwf%/C%A_Index%"%A_LoopField%"   ;Section definition
  166.     SyntaxString = %A_LoopField%             ;which Section/syntax
  167.     Gosub, ParseSyntaxString                 ;Parse through string and add to list
  168.   }
  169.  
  170. ;#############   Add or Update Wordfile   #####################################
  171.  
  172. ;Name of a file for temporary store the word file
  173. TemporaryUEwordFile = TempUEwordFile.txt
  174. FileDelete, %TemporaryUEwordFile%
  175.  
  176. Loop, Read, %UEwordfile%, %TemporaryUEwordFile%   ;Read through Wordfile
  177.   {
  178.     StringLeft, WFdef, A_LoopReadLine, 2
  179.     If WFdef = /L
  180.       {
  181.         StringSplit, WFname, A_LoopReadLine, "                  ;"
  182.         LanguageName = %WFname2%
  183.         LanguageNumber = %WFname1%
  184.         StringTrimLeft,LanguageNumber,LanguageNumber,2
  185.         If LanguageName = AutoHotkey         ;when AHK Section found, place new Section at same location
  186.           {
  187.             FileAppend, /L%LanguageNumber%%StrgAHKwf%
  188.             AHKLanguageFound := True
  189.           }
  190.       }
  191.     If LanguageName <> AutoHotkey            ;everything that does not belong to AHK, gets unchanged to file
  192.         FileAppend, %A_LoopReadLine%`n
  193.   }
  194.  
  195. If not AHKLanguageFound                      ;when AHK Section not found, append AHK Section
  196.   {
  197.     LanguageNumber += 1
  198.     FileAppend, /L%LanguageNumber%%StrgAHKwf%, %TemporaryUEwordFile%
  199.   }
  200.  
  201. FileCopy, %UEwordfile%, %UEwordfile%.ahk.bak, 1    ;Create Backup of current wordfile
  202. FileMove, %TemporaryUEwordFile%, %UEwordfile%, 1       ;Replace wordfile with temporary file
  203.  
  204. ; Tell user what has been done
  205. Question = `n`nWould you like to make UltraEdit the Default editor for AutoHotkey scripts (.ahk files)?
  206. If AHKLanguageFound
  207.     MsgBox, 4,, The AutoHotkey-Syntax for UltraEdit has been updated in your wordfile:`n`n%UEwordfile%`n`nA backup has been created in the same folder.%Question%
  208. Else
  209.     MsgBox, 4,, The AutoHotkey-Syntax for UltraEdit has been added to your wordfile:`n`n%UEwordfile%`n`nA backup has been created in the same folder.%Question%
  210.  
  211. IfMsgBox, Yes
  212.     RegWrite, REG_SZ, HKEY_CLASSES_ROOT, AutoHotkeyScript\Shell\Edit\Command,, %UeditPath%\uedit32.exe "`%1"
  213.  
  214. ExitApp  ; That's it, exit
  215.  
  216. ;#############   SubRoutines   ################################################
  217.  
  218. ReadSyntaxFromFile:
  219.   TempString =
  220.   Loop, Read , %PathSyntaxFiles%\%SyntaxFileName%%SyntaxExtention%   ;read syntax file
  221.     {
  222.       StringLeft,Char, A_LoopReadLine ,1
  223.       ;if line is comment, don't bother, otherwise add keyword to string
  224.       If Char <> `;
  225.         {
  226.           ;only add first word in line
  227.           Loop Parse, A_LoopReadLine, `,%A_Tab%%A_Space%(
  228.             {
  229.               TempString = %TempString%%A_LoopField%`n
  230.               Break
  231.             }
  232.         }
  233.     }
  234.   %SyntaxFileName% = %TempString%                          ;Assign string to syntax filename
  235.   Sort, %SyntaxFileName%, U                                ;Sort keywords in string
  236. Return
  237.  
  238. ParseSyntaxString:
  239.   Loop, Parse, %SyntaxString%, `n                 ;Parse through syntax string
  240.     {
  241.       StringLeft, Char, A_LoopField,1
  242.       If (Char = PrevChar)                       ;add keyword to line when first character is same with previous keyword
  243.           StrgAHKwf = %StrgAHKwf% %A_LoopField%
  244.       Else                                       ;for every keyword with a new first letter, start a new row
  245.           StrgAHKwf = %StrgAHKwf%`n%A_LoopField%
  246.       PrevChar = %Char%                          ;remember first character of keyword
  247.     }
  248. Return
  249.  
  250. ;#############   END of File   ################################################
  251.